package com.example.assetmanagement.controller;

import com.example.assetmanagement.common.ApiResponse;
import com.example.assetmanagement.common.PageRequest;
import com.example.assetmanagement.common.PageResponse;
import com.example.assetmanagement.domain.Maintenance;
import com.example.assetmanagement.service.MaintenanceService;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/maintenance")
@RequiredArgsConstructor
public class MaintenanceController {

    private final MaintenanceService maintenanceService;

    @GetMapping("/list")
    public ApiResponse<PageResponse<Maintenance>> list(@Valid PageRequest pageRequest,
                                                       @RequestParam(required = false) Long assetId,
                                                       @RequestParam(required = false) String assetName,
                                                       @RequestParam(required = false) Integer status,
                                                       @RequestParam(required = false) String type,
                                                       @RequestParam(required = false) Integer priority,
                                                       @RequestParam(required = false) String startDate,
                                                       @RequestParam(required = false) String endDate) {
        return ApiResponse.success(maintenanceService.list(pageRequest, assetId, assetName, status, type, priority, startDate, endDate));
    }

    @GetMapping("/{id}")
    public ApiResponse<Maintenance> detail(@PathVariable Long id) {
        return ApiResponse.success(maintenanceService.getById(id));
    }

    @PostMapping
    public ApiResponse<?> add(@RequestBody @Valid Maintenance maintenance) {
        maintenanceService.add(maintenance);
        return ApiResponse.success(null);
    }

    @PutMapping("/{id}")
    public ApiResponse<?> update(@PathVariable Long id, @RequestBody @Valid Maintenance maintenance) {
        maintenanceService.update(id, maintenance);
        return ApiResponse.success(null);
    }

    @DeleteMapping("/{id}")
    public ApiResponse<?> delete(@PathVariable Long id) {
        maintenanceService.delete(id);
        return ApiResponse.success(null);
    }

    @DeleteMapping("/batch")
    public ApiResponse<?> batchDelete(@RequestBody List<Long> ids) {
        maintenanceService.batchDelete(ids);
        return ApiResponse.success(null);
    }

    @PutMapping("/{id}/status")
    public ApiResponse<?> updateStatus(@PathVariable Long id, @RequestParam Integer status) {
        maintenanceService.updateStatus(id, status);
        return ApiResponse.success(null);
    }

    @GetMapping("/asset/{assetId}")
    public ApiResponse<List<Maintenance>> listByAsset(@PathVariable Long assetId) {
        return ApiResponse.success(maintenanceService.getByAsset(assetId));
    }

    @GetMapping("/stats")
    public ApiResponse<Map<String, Object>> stats() {
        return ApiResponse.success(maintenanceService.stats());
    }
} 